package com.dlc.shop.systemtest.data.amount;

import com.dlc.shop.bean.app.dto.OrderItemDto;
import com.dlc.shop.bean.app.dto.OrderShopDto;
import com.dlc.shop.bean.app.dto.UserDto;
import com.dlc.shop.bean.app.param.OrderRefundParam;
import com.dlc.shop.systemtest.api.amount.UserApi;
import com.dlc.shop.systemtest.api.order.MyOrderApi;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author lanhai
 * 订单退款测试数据
 */
public class OrderRefundData {


    /**
     * 整单退款
     * 仅退款
    * */
    public static OrderRefundParam getOrderRefundParam(String orderNumber){
        OrderRefundParam orderRefundParam = new OrderRefundParam();
        OrderShopDto orderShopDto = MyOrderApi.orderDetail(orderNumber);
        UserDto userDto = UserApi.userInfo();
        orderRefundParam.setApplyType(1);
        orderRefundParam.setBuyerDesc("123");
        orderRefundParam.setBuyerMobile(userDto.getMobile());
        orderRefundParam.setBuyerReason("1");
        orderRefundParam.setGoodsNum(orderShopDto.getTotalNum());
        orderRefundParam.setIsReceiver(false);
        orderRefundParam.setOrderNumber(orderNumber);
        orderRefundParam.setRefundAmount(orderShopDto.getCanRefundAmount());
        orderRefundParam.setRefundType(1);
        return  orderRefundParam;
    }

    /**
     * 部分退款
     * @param orderNumber 订单编号
     * @param orderItemId 订单项id
     * @param refundAmount 退款金额
     * @return
     */
    public static OrderRefundParam getRebatesOrderRefundParam(String orderNumber, Long orderItemId, Double refundAmount, Integer applyType, Integer goodsNum){
        OrderRefundParam orderRefundParam = new OrderRefundParam();
        OrderShopDto orderShopDto = MyOrderApi.orderDetail(orderNumber);
        Map<Long, List<OrderItemDto>> collectMap = orderShopDto.getOrderItemDtos().stream().collect(Collectors.groupingBy(OrderItemDto::getOrderItemId));
        List<OrderItemDto> orderItemDtos = collectMap.get(orderItemId);
        UserDto userDto = UserApi.userInfo();
        orderRefundParam.setApplyType(applyType);
        orderRefundParam.setBuyerDesc("123");
        orderRefundParam.setBuyerMobile(userDto.getMobile());
        orderRefundParam.setBuyerReason("1");
        orderRefundParam.setGoodsNum(orderItemDtos.get(0).getProdCount());
        orderRefundParam.setIsReceiver(false);
        orderRefundParam.setOrderNumber(orderNumber);
        orderRefundParam.setRefundType(1);
        orderRefundParam.setOrderItemId(orderItemId);
        orderRefundParam.setRefundAmount(orderItemDtos.get(0).getActualTotal());
        orderRefundParam.setGoodsNum(goodsNum);
        if (Objects.nonNull(refundAmount)){
            orderRefundParam.setRefundAmount(refundAmount);
        }
        orderRefundParam.setRefundType(2);
        return  orderRefundParam;
    }

    /**
     * 有赠品的商品部分退款
     */
    public static OrderRefundParam getRebatesOrderRefundParam(String orderNumber, Long orderItemId, Double refundAmount, Integer applyType, Integer goodsNum, List<Long> giveawayItemIds){
        OrderRefundParam orderRefundParam = new OrderRefundParam();
        OrderShopDto orderShopDto = MyOrderApi.orderDetail(orderNumber);
        Map<Long, List<OrderItemDto>> collectMap = orderShopDto.getOrderItemDtos().stream().collect(Collectors.groupingBy(OrderItemDto::getOrderItemId));
        List<OrderItemDto> orderItemDtos = collectMap.get(orderItemId);
        UserDto userDto = UserApi.userInfo();
        orderRefundParam.setApplyType(applyType);
        orderRefundParam.setBuyerDesc("123");
        orderRefundParam.setBuyerMobile(userDto.getMobile());
        orderRefundParam.setBuyerReason("1");
        orderRefundParam.setGoodsNum(orderItemDtos.get(0).getProdCount());
        orderRefundParam.setIsReceiver(false);
        orderRefundParam.setOrderNumber(orderNumber);
        orderRefundParam.setRefundType(1);
        orderRefundParam.setOrderItemId(orderItemId);
        orderRefundParam.setRefundAmount(orderItemDtos.get(0).getActualTotal());
        orderRefundParam.setGiveawayItemIds(giveawayItemIds);
        orderRefundParam.setGoodsNum(goodsNum);
        if (Objects.nonNull(refundAmount)){
            orderRefundParam.setRefundAmount(refundAmount);
        }
        orderRefundParam.setRefundType(2);
        return  orderRefundParam;
    }

    /**
     * 整单退款
     * applyType：1仅退款，2退货退款
     * */
    public static OrderRefundParam getOrderRefundParam(String orderNumber,Integer applyType){
        OrderRefundParam orderRefundParam = new OrderRefundParam();
        OrderShopDto orderShopDto = MyOrderApi.orderDetail(orderNumber);
        UserDto userDto = UserApi.userInfo();
        orderRefundParam.setApplyType(applyType);
        orderRefundParam.setBuyerDesc("退款说明");
        orderRefundParam.setBuyerMobile(userDto.getMobile());
        orderRefundParam.setBuyerReason("1");
        orderRefundParam.setGoodsNum(orderShopDto.getTotalNum());
        orderRefundParam.setIsReceiver(true);
        orderRefundParam.setOrderNumber(orderNumber);
        orderRefundParam.setRefundAmount(orderShopDto.getCanRefundAmount());
        orderRefundParam.setRefundType(1);
        return  orderRefundParam;
    }
    /**
     * 部分退款
     * 订单第一项申请退款
     * */
    public static OrderRefundParam getSingleOrderRefundParam(String orderNumber){
        OrderRefundParam orderRefundParam = new OrderRefundParam();
        OrderShopDto orderShopDto = MyOrderApi.orderDetail(orderNumber);
        orderRefundParam.setApplyType(1);
        orderRefundParam.setBuyerDesc("退款说明1");
        orderRefundParam.setBuyerReason("0");
        orderRefundParam.setGoodsNum(orderShopDto.getTotalNum());
        orderRefundParam.setIsReceiver(true);
        orderRefundParam.setOrderItemId(orderShopDto.getOrderItemDtos().get(0).getOrderItemId());
        orderRefundParam.setOrderNumber(orderNumber);
        orderRefundParam.setRefundAmount(orderShopDto.getCanRefundAmount());
        orderRefundParam.setRefundType(2);
        return  orderRefundParam;
    }

    public static OrderRefundParam getSingleOrderPartRefundParam(String orderNumber,Double refundAmount){
        OrderRefundParam orderRefundParam = new OrderRefundParam();
        OrderShopDto orderShopDto = MyOrderApi.orderDetail(orderNumber);
        orderRefundParam.setApplyType(1);
        orderRefundParam.setBuyerDesc("退款说明1");
        orderRefundParam.setBuyerReason("0");
        orderRefundParam.setGoodsNum(orderShopDto.getTotalNum());
        orderRefundParam.setIsReceiver(true);
        orderRefundParam.setOrderItemId(orderShopDto.getOrderItemDtos().get(0).getOrderItemId());
        orderRefundParam.setOrderNumber(orderNumber);
        orderRefundParam.setRefundAmount(refundAmount);
        orderRefundParam.setRefundType(2);
        return  orderRefundParam;
    }
}
